function convert(number) {
    if (number == 1) {
        return "<span style='color:green;'>bật</span>";
    } else {
        return "<span style='color:red;'>tắt</span>";
    }
}
//show thêm sub
function showthemsub() {
    var formsua = "<table class='table'>";
    formsua += "<tr>";
    formsua += "<td'><select class='form-control' name='opchuyenmuc' id='opchuyenmuc'>";
    var url2 = "admin/getchuyenmuc";
    $.get(url2, function(data, status) {
        var txt = "";
        $.each(data, function(key, value) {
            txt += "<option value='" + value.category_id + "'>" + value.category_name + "</option>";
        });
        $("#opchuyenmuc").append(txt);
    }, "json");
    formsua += "</select></td>";
    formsua += "</tr><tr>";
    formsua += "<td><input type='text' class='form-control' id='sub_category_name' name=sub_'category_name'  /></td> ";
    formsua += "</tr><tr>";
    formsua += "<td><input type='text' class='form-control' id='sub_category_desc' name='sub_category_desc'  /></td> ";
    formsua += "</tr><tr>";
    formsua += "<td><input type='text' class='form-control' id='sub_category_links' name='sub_category_links'  /></td> ";
    formsua += "</tr><tr>";
    formsua += "<td><input type='checkbox' name='status'' onclick='changevalue();' id='status' value='1' /> Bật / Tắt</td> ";
    formsua += "</tr>";
    formsua += "</table>";
    $("#tieude").text("Thêm chuyên mục con");
    $("#tinnhan").html(formsua);
    $("#btmsua").text("Đóng");
    $("#btmxoa").text("Thêm chuyên mục con");
    $("#btmxoa").attr("onclick", "themsubchuyenmuc();");
}
//Show thêm
function showthem() {
    var formsua = "<table class='table'>";
    formsua += "<input type='hidden' name='category_id' id='category_id' />";
    formsua += "<tr>";
    formsua += "<td><input type='text' class='form-control' id='category_name' name='category_name' placeholder='Tên chuyên mục' /></td> ";
    formsua += "</tr><tr>";
    formsua += "<td><input type='text' class='form-control' id='category_desc' name='category_desc' placeholder='Chú thích chuyên mục' /></td> ";
    formsua += "</tr><tr>";
    formsua += "<td><input type='text' class='form-control' id='class' name='class' placeholder='Class chuyên mục'/> </td>";
    formsua += "</tr><tr>";
    formsua += "<td><input type='text' class='form-control' id='category_links' name='category_links' placeholder='Đường dẫn chuyên mục'  /></td> ";
    formsua += "</tr><tr>";
    formsua += "<td><input type='checkbox' name='status'' onclick='changevalue();' id='status' value='1' /> Bật / Tắt</td> ";
    formsua += "</tr>";
    formsua += "</table>";
    $("#tieude").text("Thêm chuyên mục");
    $("#tinnhan").html(formsua);
    $("#btmsua").text("Đóng");
    $("#btmxoa").text("Thêm chuyên mục");
    $("#btmxoa").attr("onclick", "themchuyenmuc();");
}
//end show them
//viết function checked unchecked
function checked(id) {
    if (id == '1') {
        return "checked='checked'";
    } else {
        return "";
    }
}
function changevalue() {
    var check = $('input[name=status]').is(':checked');
    if (check == true) {
        $("#status").attr("value", "1");
    } else {
        $("#status").attr("value", "0");
    }
}
//function thêm sub chuyên mục
function themsubchuyenmuc() {
    var url = "admin/themsubchuyenmuc";
    var data = {
        "sub_category_name": $("#sub_category_name").val(),
        "sub_category_desc": $("#sub_category_desc").val(),
        "opchuyenmuc": $("#opchuyenmuc").val(),
        "sub_category_links": $("#sub_category_links").val(),
        "status": $("#status").val(),
        "sub_category_id": $("#sub_category_id").val()
    };
    $.post(url, data, function(value, status) {
        showdatasub();
    }, "json");
}
//Thêm chuyên mục 
function themchuyenmuc() {
    var url = "admin/themchuyenmuc";
    var data = {
        "category_name": $("#category_name").val(),
        "category_desc": $("#category_desc").val(),
        "class": $("#class").val(),
        "category_links": $("#category_links").val(),
        "status": $("#status").val(),
        "category_id": $("#category_id").val()
    };
    $.post(url, data, function(value, status) {
        showdata();
    }, "json");
}
//Save datasub
function savedatasub() {
    var url = "admin/suasubchuyenmuc";
    var data = {
        "sub_category_name": $("#sub_category_name").val(),
        "sub_category_desc": $("#sub_category_desc").val(),
        "opchuyenmuc": $("#opchuyenmuc").val(),
        "sub_category_links": $("#sub_category_links").val(),
        "status": $("#status").val(),
        "sub_category_id": $("#sub_category_id").val()
    };
    $.post(url, data, function(value, status) {
        showdatasub();
    }, "json");
}

///Khi người dùng nhấn sửa 
function savedata() {
    var url = "admin/suachuyenmuc";
    var data = {
        "category_name": $("#category_name").val(),
        "category_desc": $("#category_desc").val(),
        "class": $("#class").val(),
        "category_links": $("#category_links").val(),
        "status": $("#status").val(),
        "category_id": $("#category_id").val()
    };
    $.post(url, data, function(value, status) {
        showdata();
    }, "json");
}

//start function showsuasub
function showsuasub(idsua) {
    var url = "admin/getidsuasub/" + idsua;
    $.get(url, function(data, status) {
        var formsua = "<table class='table'>";
        formsua += "<input type='hidden' name='sub_category_id' id='sub_category_id' value='" + data[0].sub_category_id + "' />";
        formsua += "<tr>";
        formsua += "<td'><select class='form-control' name='opchuyenmuc' id='opchuyenmuc'>";
        var url2 = "admin/getchuyenmuc";
        $.get(url2, function(data, status) {
            var txt = "";
            $.each(data, function(key, value) {
                txt += "<option value='" + value.category_id + "'>" + value.category_name + "</option>";
            });
            $("#opchuyenmuc").append(txt);
        }, "json");
        formsua += "</select></td>";
        formsua += "</tr><tr>";
        formsua += "<td><input type='text' class='form-control' id='sub_category_name' name=sub_'category_name' value='" + data[0].sub_category_name + "' /></td> ";
        formsua += "</tr><tr>";
        formsua += "<td><input type='text' class='form-control' id='sub_category_desc' name='sub_category_desc' value='" + data[0].sub_category_desc + "' /></td> ";
        formsua += "</tr><tr>";
        formsua += "<td><input type='text' class='form-control' id='sub_category_links' name='sub_category_links' value='" + data[0].sub_category_links + "' /></td> ";
        formsua += "</tr><tr>";
        formsua += "<td><input type='checkbox' name='status'' onclick='changevalue();' id='status' value='1'" + checked(data[0].sub_category_status) + " /> Bật / Tắt</td> ";
        formsua += "</tr>";
        formsua += "</table>";
        $("#tieude").text("Chỉnh sửa  chuyên mục con");
        $("#tinnhan").html(formsua);
        $("#btmsua").text("Sửa");
        $("#btmxoa").text("Xóa chuyên mục con");
        $("#btmsua").attr("onclick", "savedatasub();");
        $("#btmxoa").attr("onclick", "xoasub(" + data[0].sub_category_id + ")");
    }, "json");
}
//Start function show sửa
function showsua(idsua) {
    var url = "admin/getidsua/" + idsua;
    $.get(url, function(data, status) {
        var formsua = "<table class='table'>";
        formsua += "<input type='hidden' name='category_id' id='category_id' value='" + data[0].category_id + "' />";
        formsua += "<tr>";
        formsua += "<td><input type='text' class='form-control' id='category_name' name='category_name' value='" + data[0].category_name + "' /></td> ";
        formsua += "</tr><tr>";
        formsua += "<td><input type='text' class='form-control' id='category_desc' name='category_desc' value='" + data[0].category_desc + "' /></td> ";
        formsua += "</tr><tr>";
        formsua += "<td><input type='text' class='form-control' id='class' name='class' value='" + data[0].class + "' /> </td>";
        formsua += "</tr><tr>";
        formsua += "<td><input type='text' class='form-control' id='category_links' name='category_links' value='" + data[0].category_links + "' /></td> ";
        formsua += "</tr><tr>";
        formsua += "<td><input type='checkbox' name='status'' onclick='changevalue();' id='status' value='1'" + checked(data[0].category_status) + " /> Bật / Tắt</td> ";
        formsua += "</tr>";
        formsua += "</table>"
        $("#tieude").text("Chỉnh sửa chuyên mục");
        $("#tinnhan").html(formsua);
        $("#btmsua").text("Sửa");
        $("#btmxoa").text("Xóa chuyên mục");
        $("#btmsua").attr("onclick", "savedata();");
        $("#btmxoa").attr("onclick", "xoa(" + data[0].category_id + ")");
    }, "json");
}
function showxoasub(id) {
    $("#tieude").text("Thông báo");
    $("#tinnhan").html("Bạn có muôn xóa chuyên mục con có sub_category_id là : " + id + " không ?");
    $("#btmxoa").text("Xóa chuyên mục con");
    $("#btmxoa").attr("onclick", "xoasub(" + id + ")");
    $("#btmsua").text("Đóng");
}
function showxoa(id) {
    $("#tieude").text("Thông báo");
    $("#tinnhan").html("Bạn có muôn xóa chuyên mục có category_id là : " + id + " không ?");
    $("#btmxoa").text("Xóa chuyên mục");
    $("#btmxoa").attr("onclick", "xoa(" + id + ")");
    $("#btmsua").text("Đóng");
}
function showdatasub() {
    var urlsubchuyenmuc = "admin/getsubchuyenmuc";
    $.get(urlsubchuyenmuc, function(data, status) {
        var txt = "<table class='table table-condensed'><tr><td class='active'>ID</td><td class='info'>Chuyên mục mẹ</td><td class='success'>Tên chuyên mục con</td><td class='info'>Chú thích chuyên mục con</td><td class='warning'>Đường dẫn chuyên mục con</td><td class='danger'>Trạng thái</td><td class='active'>Sửa</td><td class='info'>Xóa</td></tr>";
        $.each(data, function(key, value) {

            txt += "<tr><td>" + value.sub_category_id + "</td><td>" + value.category_id + "</td><td>" + value.sub_category_name + "</td><td>" + value.sub_category_desc + "</td><td>" + value.sub_category_links + "</td><td>" + convert(value.sub_category_status) + "</td><td><a data-toggle='modal' data-target='#showinfo' onclick='javascript:showsuasub(" + value.sub_category_id + ")' href='#' class='glyphicon glyphicon-edit'></a></td><td><a data-toggle='modal' data-target='#showinfo' onclick='showxoasub(" + value.sub_category_id + ")'  href='#' class='glyphicon glyphicon-remove'></a></td></tr>";

        });
        txt += "</table>"
        txt += "<button type='button' data-toggle='modal' data-target='#showinfo' onclick='showthemsub();' id='themchuyenmuccon' class='btn btn-success'>Thêm chuyên mục con</button> ";
        $("#show").html(txt);
    }, "json");
}
function showdata() {
    var url2 = "admin/getchuyenmuc";
    $.get(url2, function(data, status) {
        var txt = "<table class='table table-condensed'><tr><td class='active'>ID</td><td class='success'>Tên chuyên mục</td><td class='info'>Chú thích chuyên mục</td><td class='warning'>Đường dẫn chuyên mục</td><td class='danger'>Trạng thái</td><td class='active'>Sửa</td><td class='info'>Xóa</td></tr>";
        $.each(data, function(key, value) {
            txt += "<tr><td>" + value.category_id + "</td><td>" + value.category_name + "</td><td>" + value.category_desc + "</td><td>" + value.category_links + "</td><td>" + convert(value.category_status) + "</td><td><a data-toggle='modal' data-target='#showinfo' onclick='javascript:showsua(" + value.category_id + ")' href='#' class='glyphicon glyphicon-edit'></a></td><td><a data-toggle='modal' data-target='#showinfo' onclick='showxoa(" + value.category_id + ")'  href='#' class='glyphicon glyphicon-remove'></a></td></tr>";
        });
        txt += "</table>";
        txt += "<button type='button' data-toggle='modal' data-target='#showinfo'  onclick='showthem();' id='themsanpham' class='btn btn-success'>Thêm chuyên mục</button> ";
        $("#show").html(txt);
    }, "json");
}
function xoasub(idxoa) {
    var url = "admin/xoasubchuyenmuc/" + idxoa;
    $.get(url, function(data, status, jqXHR) {
        showdatasub();
    }, "json");
    showdatasub();
}
function xoa(idxoa) {
    var url = "admin/xoachuyenmuc/" + idxoa;
    $.get(url, function(data, status, jqXHR) {
        showdata();
    }, "json");
    showdata();

}
// phần chung
$(document).ready(function() {
    //Start quản lý chuyên mục
    $("#qlchuyenmuc").click(function() {

        showdata();
    });
    //end quản lý chuyên muc
    //start quan ly sub chuyen muc
    $("#qlsubchuyenmuc").click(function() {
        showdatasub();
    });
    $("#qltintuc").click(function(){
       showdatatintuc(); 
    });
});


//Xong phần quản lý chuyên mục
function showdatatintuc(){
    var url2 = "admin/gettintuc";
    $.get(url2, function(data, status) {
        var txt = "<table class='table table-condensed'><tr><td class='active'>ID</td><td class='success'>Tên chuyên mục</td><td class='info'>Chú thích chuyên mục</td><td class='warning'>Đường dẫn chuyên mục</td><td class='danger'>Trạng thái</td><td class='active'>Sửa</td><td class='info'>Xóa</td></tr>";
        $.each(data, function(key, value) {

            txt += "<tr><td>" + value.category_id + "</td><td>" + value.category_name + "</td><td>" + value.category_desc + "</td><td>" + value.category_links + "</td><td>" + convert(value.category_status) + "</td><td><a data-toggle='modal' data-target='#showinfo' onclick='javascript:showsua(" + value.category_id + ")' href='#' class='glyphicon glyphicon-edit'></a></td><td><a data-toggle='modal' data-target='#showinfo' onclick='showxoa(" + value.category_id + ")'  href='#' class='glyphicon glyphicon-remove'></a></td></tr>";
        });
        txt += "</table>";
        txt += "<button type='button' data-toggle='modal' data-target='#showinfo'  onclick='showthem();' id='themsanpham' class='btn btn-success'>Thêm chuyên mục</button> ";
        $("#show").html(txt);
    }, "json");
}